package com.whg.demo.controller;

import com.whg.demo.biz.task.TestAsync;
import com.whg.demo.model.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @Author whg
 * @create 2022/12/9 10:14
 * @description：
 */
@RestController
@RequestMapping("/async")
public class AsyncController {

    @Autowired
    private TestAsync testAsync;

    @GetMapping("/execute")
    public JsonData execute() throws InterruptedException {
        long start = System.currentTimeMillis();
        /*testAsync.task1();
        testAsync.task2();
        testAsync.task3();*/
        Future<String> result1 = testAsync.task4();
        Future<String> result2 = testAsync.task5();
        Future<String> result3 = testAsync.task6();
        for(;;){
            if(result1.isDone() && result2.isDone() && result3.isDone()){
                break;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("执行总耗时：" + (end - start));
        return JsonData.buildSuccess();
    }

    @GetMapping("/testAsync")
    public JsonData testAsync() throws InterruptedException {
        testAsync.task6();
        return JsonData.buildSuccess();
    }


}
